package com.gao.controller;


import com.gao.dto.AnswerDto;
import com.gao.dto.ConsultationDto;
import com.gao.entity.Answer;
import com.gao.entity.Consultation;
import com.gao.entity.Result;
import com.gao.service.AnswerService;
import com.gao.service.ConsultationService;
import com.gao.service.UserService;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/consultation")
public class ConsultationController {
    @Autowired
    private ConsultationService consultationService;
    @Autowired
    private AnswerService   answerService;
    @Autowired
    private UserService userService;
    @PostMapping("/add")
    public Result add(@RequestBody Consultation consultation){
        return Result.success(consultationService.save(consultation));
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        answerService.remove(new QueryWrapper().eq("consultation_id",id));
        return Result.success(consultationService.removeById(id));
    }
    @PostMapping("/comment")
    public Result comment(@RequestBody Answer answer){
        System.out.println(answer.toString());
        return Result.success(answerService.save(answer));
    }
    @GetMapping("/listAl")
    public Result listAl(){
        List<Consultation> consultationList = consultationService.selectAllWithRelations();
        return Result.success(consultationList);
    }
    @GetMapping("/listAll")
    public Result listAll(@RequestParam Integer pageNumber, @RequestParam Integer pageSize){
        Page<Consultation> consultationList = consultationService.paginate(pageNumber, pageSize);
        return Result.success(consultationList);
    }
//    @GetMapping("/listAll")
//    public Result listAll(){
//        List<Consultation> consultationList = consultationService.selectAllWithRelations();
//        List<ConsultationDto> consultationDtoList = new ArrayList<>();
//        for (Consultation consultation : consultationList) {
//            ConsultationDto consultationDto = new ConsultationDto();
//            BeanUtils.copyProperties(consultation,consultationDto);
//            consultationDto.setUserName(userService.getById(consultation.getUserId()).getUsername());
//            //查评论
//            List<AnswerDto> answerDtoList = new ArrayList<>();
//            for (Answer answer : consultation.getAnswers()) {
//                AnswerDto answerDto = new AnswerDto();
//                BeanUtils.copyProperties(answer,answerDto);
//                answerDto.setUserName(userService.getById(answer.getUserId()).getUsername());
//                answerDtoList.add(answerDto);
//            }
//            consultationDto.setAnswers(answerDtoList);
//            consultationDtoList.add(consultationDto);
//        }
//       return Result.success(consultationDtoList);
//    }
//    @GetMapping("/list")
//    public Result list(){
//        return Result.success(consultationService.list());
//    }
//    @GetMapping("/list")
//    public Result list(){
//        List<Consultation> consultationList = consultationService.list();
//        List<ConsultationDto> consultationDtoList = new ArrayList<>();
//        for (Consultation consultation : consultationList) {
//            ConsultationDto consultationDto = new ConsultationDto();
//            BeanUtils.copyProperties(consultation,consultationDto);
//            //查评论
//            QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("consultation_id",consultation.getId());
//            List<AnswerDto> answerDtoList = new ArrayList<>();
//            List<Answer> answers = answerService.list(queryWrapper);
//
//            for (Answer answer : answers) {
//                AnswerDto answerDto = new AnswerDto();
//                BeanUtils.copyProperties(answer,answerDto);
//                answerDto.setUserName(userService.getById(answerDto.getUserId()).getUsername());
//                answerDtoList.add(answerDto);
//            }
//            consultationDto.setAnswers(answerDtoList);
//            consultationDto.setUserName(userService.getById(consultation.getUserId()).getUsername());
//
//            consultationDtoList.add(consultationDto);
//        }
//        return Result.success(consultationDtoList);
//    }
}
